// MovingPlugin.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "MovingPlugin.h"
#include "..\..\Shared\ObjectManager.h"

__declspec(dllexport) IPlugin* __stdcall CreateInstance() // needed for pluginmanager
{
	return (IPlugin*)new MovingPlugin(); // returns a newly created instance of the ExamplePlugin
}

__declspec(dllexport) int __stdcall GetInterfaceVersion(void) // Needed for pluginmanager
{
	return PluginInterfaceVersion; // returns the Plugin Interface Version. 
}

MovingPlugin::MovingPlugin()
{
	m_ModuleInfo.pluginName = "MovingPlugin"; // Change to the name of your plugin
	m_ModuleInfo.author = "Kevin Lawrence"; // Change to your name
	m_ModuleInfo.version = 0x01000000; // 0xMMmmaabb   MM = Major mm = Minor aa = Macro bb = Build
	m_ModuleInfo.numOfCommands = 1; // Needs to equal the number of commands that your plugin handles.
	m_ModuleInfo.commands[0] = "Move";
}

bool __stdcall MovingPlugin::HandleCommand(Command &NewCommand)
{
	//std::cerr << "Starting HandleCommand" << std::endl;
	if ( NewCommand.name == "Move")
	{
		OnMove(NewCommand);
		return true;
	}
	return false;
}

void MovingPlugin::OnMove(Command &NewCommand)
{
	//std::cout << "Starting OnMove" << std::endl;
	if(NewCommand.Arguments.Size() == 3)
	{
		//std::cout << "Inside first IF" << std::endl;
		// Add 0's x and the value in 1 -> x
		Value &Temp = (*m_Objects)[NewCommand.Arguments[0]].GetBaseAttribute("Name");
		//std::cout << Temp << std::endl;
		Value x = (*m_Objects)[NewCommand.Arguments[0]].GetBaseAttribute("X") + NewCommand.Arguments["X"];
		int x1 = (*m_Objects)[NewCommand.Arguments[0]].GetBaseAttribute("X").ToInt() + NewCommand.Arguments["X"].ToInt();
		// Add 0's y and the value in 2 -> y
		Value y = (*m_Objects)[NewCommand.Arguments[0]].GetBaseAttribute("Y") + NewCommand.Arguments["Y"];
		// Get the value on the map at x and y
		Value passable = (*m_Objects)[NewCommand.Arguments[0]].GetBaseAttribute("PASSABLE");
		//std::cout << x << ", " << y << " | " << passable << std::endl;
		char PosName[7];
		itoa(x.ToInt(), PosName, 10);
		PosName[strlen(PosName) + 1] = '\0';
		PosName[strlen(PosName)] = ',';
		itoa(y.ToInt(), &PosName[strlen(PosName)], 10);

		int test = (*m_Objects)["Map"].GetBaseAttribute(PosName).ToInt();
		int test2 = passable.ToInt();

		if (((*m_Objects)["Map"].GetBaseAttribute(PosName).ToInt() & passable.ToInt()))
		{
			(*m_Objects)[NewCommand.Arguments[0]].GetBaseAttribute("X") = x;
			(*m_Objects)[NewCommand.Arguments[0]].GetBaseAttribute("Y") = y;
			NewCommand.Returns.Add(Value("Code", 0));
			(*m_Objects)[NewCommand.Arguments[0]].GetBaseAttribute("CurSpeed") -= 5;
		}
		else
		{
			NewCommand.Returns.Add(Value("Code", -1));
			NewCommand.Returns.Add(Value("Msg", "You can't move there."));
		}
		// Check if value is stored in list of enterable terrain in 0
		// If value is present
		// change 0's x by 1's value
		// change 0's y by 2's value
	//std::cout << "Ending First IF" << std::endl;
	}
	
	//std::cout << "Finishing OnMove" << std::endl;
}